tailwind.config = {
    theme: {
        extend: {
            colors: {
                primary: '#165DFF',
                secondary: '#36CFC9',
                accent: '#722ED1',
                neutral: '#F5F7FA',
                dark: '#1D2129',
                success: '#52C41A',
                warning: '#FAAD14',
                danger: '#F5222D'
            },
            fontFamily: {
                inter: ['Inter', 'system-ui', 'sans-serif'],
            },
        }
    }
}
document.addEventListener('DOMContentLoaded', function() {
    // 验证用户登录状态
    const users = JSON.parse(localStorage.getItem('registeredUsers')) || [];
    const loginEmail = localStorage.getItem('currentLoginEmail');
    
    if (!loginEmail) {
        window.location.href = 'index.html';
        return;
    }
    
    // 获取当前用户信息
    const currentUser = users.find(user => user.email === loginEmail);
    if (currentUser) {
        // 更新导航栏用户信息
        document.getElementById('user-display-name').textContent = currentUser.name || '用户';
        if (currentUser.avatar) {
            document.getElementById('header-avatar').src = currentUser.avatar;
        }
    } else {
        localStorage.removeItem('currentLoginEmail');
        window.location.href = 'index.html';
    }
    
    // 移动端菜单切换
    const mobileMenuButton = document.getElementById('mobile-menu-button');
    const mobileMenu = document.getElementById('mobile-menu');
    
    mobileMenuButton.addEventListener('click', function() {
        mobileMenu.classList.toggle('hidden');
    });
    
    // 服务权限数据
    const servicePermissions = {
        google: {
            title: "连接 Google",
            message: "连接 Google 后，UserHub 将能够访问您的日历和邮件数据，以便提供更好的任务提醒服务。",
            permissions: ["查看和管理您的日历事件", "发送邮件提醒"]
        },
        microsoft: {
            title: "连接 Microsoft",
            message: "连接 Microsoft 后，UserHub 将能够访问您的Office 365数据，实现文档和任务的同步。",
            permissions: ["查看您的OneDrive文档", "同步Outlook日历"]
        },
        whatsapp: {
            title: "连接 WhatsApp",
            message: "连接 WhatsApp 后，UserHub 将能够通过WhatsApp发送重要通知和提醒。",
            permissions: ["发送通知消息", "查看您的基本信息"]
        }
    };
    
    // 当前连接的服务
    let connectedServices = JSON.parse(localStorage.getItem('connectedServices')) || ['twitter', 'youtube'];
    let currentService = '';
    
    // 连接服务
    document.querySelectorAll('.connect-service').forEach(button => {
        button.addEventListener('click', function() {
            currentService = this.dataset.service;
            const serviceData = servicePermissions[currentService];
            
            if (serviceData) {
                document.getElementById('connection-modal-title').textContent = serviceData.title;
                document.getElementById('connection-modal-message').textContent = serviceData.message;
                
                // 更新权限列表
                const permissionList = document.getElementById('permission-list');
                permissionList.innerHTML = '';
                serviceData.permissions.forEach(permission => {
                    const li = document.createElement('li');
                    li.textContent = permission;
                    permissionList.appendChild(li);
                });
                
                document.getElementById('service-connection-modal').classList.remove('hidden');
            }
        });
    });
    
    // 确认连接
    document.getElementById('confirm-connection').addEventListener('click', function() {
        // 添加到已连接服务
        if (!connectedServices.includes(currentService)) {
            connectedServices.push(currentService);
            localStorage.setItem('connectedServices', JSON.stringify(connectedServices));
        }
        
        // 更新按钮状态
        const connectButton = document.querySelector(`.connect-service[data-service="${currentService}"]`);
        if (connectButton) {
            const parentDiv = connectButton.parentElement;
            connectButton.remove();
            
            const connectedButton = document.createElement('button');
            connectedButton.className = 'connected-service px-3 py-1 border border-success/30 bg-success/5 rounded text-sm text-success';
            connectedButton.disabled = true;
            connectedButton.innerHTML = '<i class="fa fa-check mr-1"></i>已连接';
            parentDiv.appendChild(connectedButton);
        }
        
        document.getElementById('service-connection-modal').classList.add('hidden');
        showToast(`${currentService.charAt(0).toUpperCase() + currentService.slice(1)} 连接成功`);
    });
    
    // 断开连接
    document.querySelectorAll('.disconnect-service').forEach(button => {
        button.addEventListener('click', function() {
            currentService = this.dataset.service;
            document.getElementById('disconnection-modal-title').textContent = `断开 ${currentService.charAt(0).toUpperCase() + currentService.slice(1)} 连接`;
            document.getElementById('service-disconnection-modal').classList.remove('hidden');
        });
    });
    
    // 确认断开连接
    document.getElementById('confirm-disconnection').addEventListener('click', function() {
        // 从已连接服务中移除
        connectedServices = connectedServices.filter(service => service !== currentService);
        localStorage.setItem('connectedServices', JSON.stringify(connectedServices));
        
        // 更新按钮状态
        const disconnectButton = document.querySelector(`.disconnect-service[data-service="${currentService}"]`);
        if (disconnectButton) {
            const parentDiv = disconnectButton.parentElement;
            disconnectButton.remove();
            
            const connectButton = document.createElement('button');
            connectButton.className = 'connect-service px-3 py-1 border border-gray-300 rounded text-sm text-gray-700 hover:bg-gray-50 transition-colors';
            connectButton.dataset.service = currentService;
            connectButton.textContent = '连接';
            
            // 添加点击事件
            connectButton.addEventListener('click', function() {
                const service = this.dataset.service;
                const serviceData = servicePermissions[service];
                
                if (serviceData) {
                    document.getElementById('connection-modal-title').textContent = serviceData.title;
                    document.getElementById('connection-modal-message').textContent = serviceData.message;
                    
                    // 更新权限列表
                    const permissionList = document.getElementById('permission-list');
                    permissionList.innerHTML = '';
                    serviceData.permissions.forEach(permission => {
                        const li = document.createElement('li');
                        li.textContent = permission;
                        permissionList.appendChild(li);
                    });
                    
                    document.getElementById('service-connection-modal').classList.remove('hidden');
                    currentService = service;
                }
            });
            
            parentDiv.appendChild(connectButton);
        }
        
        document.getElementById('service-disconnection-modal').classList.add('hidden');
        showToast(`${currentService.charAt(0).toUpperCase() + currentService.slice(1)} 已断开连接`);
    });
    
    // 关闭模态框
    document.querySelectorAll('.close-modal, .cancel-modal').forEach(button => {
        button.addEventListener('click', function() {
            document.getElementById('service-connection-modal').classList.add('hidden');
            document.getElementById('service-disconnection-modal').classList.add('hidden');
        });
    });
    
    // 点击模态框外部关闭
    document.getElementById('service-connection-modal').addEventListener('click', function(e) {
        if (e.target === this) {
            this.classList.add('hidden');
        }
    });
    
    document.getElementById('service-disconnection-modal').addEventListener('click', function(e) {
        if (e.target === this) {
            this.classList.add('hidden');
        }
    });
    
    // 退出登录
    document.getElementById('logout-btn').addEventListener('click', function() {
        if (confirm('确定要退出登录吗？')) {
            localStorage.removeItem('currentLoginEmail');
            window.location.href = 'index.html';
        }
    });
    
    // 显示提示消息
    function showToast(message) {
        const toast = document.getElementById('success-toast');
        document.getElementById('toast-message').textContent = message;
        
        // 显示toast
        toast.classList.remove('translate-y-20', 'opacity-0');
        toast.classList.add('translate-y-0', 'opacity-100');
        
        // 3秒后隐藏
        setTimeout(() => {
            toast.classList.remove('translate-y-0', 'opacity-100');
            toast.classList.add('translate-y-20', 'opacity-0');
        }, 3000);
    }
     // 用户菜单下拉功能
            const userMenuButton = document.getElementById('user-menu-button');
            const userMenuDropdown = document.getElementById('user-menu-dropdown');
            
            // 点击头像切换菜单显示/隐藏
            userMenuButton.addEventListener('click', function(event) {
                event.stopPropagation(); // 防止事件冒泡
                userMenuDropdown.classList.toggle('hidden');
            });
            
            // 点击页面其他地方关闭菜单
            document.addEventListener('click', function(event) {
                if (!userMenuButton.contains(event.target) && !userMenuDropdown.contains(event.target)) {
                    userMenuDropdown.classList.add('hidden');
                }
            });
            
            // 退出登录功能
            document.getElementById('logout-button').addEventListener('click', function() {
                localStorage.removeItem('currentLoginEmail');
                window.location.href = 'index.html';
            });
            
     updateAvatar();
    
    // 监听头像更新事件
    window.addEventListener('avatarUpdated', function(e) {
        const headerAvatar = document.getElementById('header-avatar');
        if (headerAvatar) {
            headerAvatar.src = e.detail.avatar;
        }
    });
    
    // 监听本地存储变化（应对多标签页情况）
    window.addEventListener('storage', function(e) {
        if (e.key === 'registeredUsers') {
            updateAvatar();
        }
    });
});
